package me.aartikov.sharedelementanimationsample.slice;


import me.aartikov.alligator.ImageTint;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.sharedelementanimationsample.ResourceTable;
import me.aartikov.sharedelementanimationsample.SampleApplication;
import me.aartikov.sharedelementanimationsample.screens.SecondScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;


@RegisterScreen(SecondScreen.class)
public class SecondMiFraction extends MiFraction implements SharedElementProvider {

    Image mKittenImageView;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_mifraction_second, container, false);
        SecondScreen screen = SampleApplication.getScreenResolver().getScreen(this);
        mKittenImageView = (Image) component.findComponentById(ResourceTable.Id_kitten_image_view);
        PixelMap pixelMap = ImageTint.getPixmap(getFractionAbility(), screen.getKittenIndex() == 0 ? ResourceTable.Media_kitten_0 : ResourceTable.Media_kitten_1);

        if (pixelMap != null) {
            PixelMapElement pixelMapElement = new PixelMapElement(pixelMap);
            pixelMapElement.setBounds(0, 0, pixelMap.getImageInfo().size.width * 3, pixelMap.getImageInfo().size.height * 3);
            ComponentContainer.LayoutConfig layoutConfig = mKittenImageView.getLayoutConfig();
            layoutConfig.width = pixelMap.getImageInfo().size.width * 3;
            mKittenImageView.setLayoutConfig(layoutConfig);
            mKittenImageView.setImageElement(pixelMapElement);
        }
        mKittenImageView.setClickedListener(v -> SampleApplication.getNavigator().goBack());
        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {

    }

    @Override
    public Component getSharedElement(AnimationData animationData) {
        return mKittenImageView;
    }

    @Override
    public String getSharedElementName(AnimationData animationData) {
        SecondScreen screen = SampleApplication.getScreenResolver().getScreen(this);
        return "kitten_" + screen.getKittenIndex();
    }
}
